package com.hongxun.modules.sys.controller;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.hongxun.common.utils.JwtUtils;
import com.hongxun.common.utils.R;
import com.hongxun.common.utils.RedisUtils;
import com.hongxun.common.utils.ShiroUtils;
import com.hongxun.modules.sys.entity.SysUserEntity;
import com.hongxun.modules.sys.service.SysUserService;

import org.apache.commons.io.IOUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录相关
 */
@RestController
public class SysLoginController {
    @Autowired
    private Producer producer;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private RedisUtils redisUtil;

    @RequestMapping("captcha.jpg")
    public void captcha(HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        // 生成文字验证码
        String text = producer.createText();
        // 生成图片验证码
        BufferedImage image = producer.createImage(text);
        // 保存到shiro session
        ShiroUtils.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY, text);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }

    /**
     * 登录
     */
    @RequestMapping(value = "/sys/login", method = RequestMethod.POST)
    public Map<String, Object> login(String username, String password, String captcha) throws IOException {
        String kaptcha = ShiroUtils.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
        if (!captcha.equalsIgnoreCase(kaptcha)) {
            return R.error("验证码不正确");
        }
        // 用户信息
        SysUserEntity user = sysUserService.queryByUserName1(username);
        if (user == null) {
            return R.error("账号不存在");
        }
        // 账号锁定
        if (user.getStatus() == 0) {
            return R.error("账号已被锁定,请联系管理员");
        }
        // 账号不存在、密码错误
        if (!user.getPassword().equals(new Sha256Hash(password, user.getSalt()).toHex())) {
            int count = redisUtil.get(username) == null ? 0 : Integer.parseInt(redisUtil.get(username)) + 1;
            redisUtil.set(username, count);
            if (count >= 5) {//锁定账号
                user.setStatus(0);
                user.setPassword(null);
                sysUserService.update(user);
            }
            return R.error("账号或密码不正确");
        }
        System.out.println("user:" + user);
        System.out.println("getPassword:" + user.getPassword());
        System.out.println("getSalt:" + new Sha256Hash(password, user.getSalt()).toHex());

        redisUtil.set(username, 0);
        // 生成token
        String token = jwtUtils.generateToken(user.getUserId());

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", jwtUtils.getExpire());

        redisUtil.set(token, user, jwtUtils.getExpire());
        return R.ok(map);
    }

}
